<?php
namespace app\api\controller;

use app\common\model\SmsCodeModel;
use app\common\model\UserModel;
use app\middleware\Auth;

/**
 * 登录控制器
 * @author 贺强
 * @time   2021-10-21 10:10:55
 */
class Login extends Common
{
    protected $middleware = [Auth::class];

    /**
     * 用户登录
     * @author 贺强
     * @time   2021-10-21 10:33:57
     */
    public function index()
    {
        $param = $this->request->post();
        if (empty($param['code']) && empty($param['password']) || empty($param['mobile'])) {
            return ajax(301, '参数缺失');
        }
        if (!empty($param['code'])) {
            $sc   = new SmsCodeModel();
            $time = $sc->getFieldValue('ctime', ['mobile' => $param['mobile'], 'code' => $param['code']], ['ctime' => 'desc']);
            if (empty($time)) {
                return ajax(302, '无效验证码');
            }
            $time += config('app.CODE_VALIDITY');
            if ($time < time()) {
                return ajax(303, '验证码过期');
            }
            $sc->delByWhere(['mobile' => $param['mobile']]);
        }
        $type = 1;
        $u    = new UserModel();
        $user = $u->getModel(['mobile|username' => $param['mobile']], ['id', 'username', 'mobile', 'password']);
        // 如果验证码为空则是密码登录
        if (empty($param['code'])) {
            if (!password_verify($param['password'], $user['password'])) {
                return ajax(304, '用户名或密码错误');
            }
        }
        if (empty($user)) {
            $id   = $u->add(['mobile' => $param['mobile']]);
            $user = $u->getModel(['id' => $id]);
            $type = 2;
        }
        // 赋值登录时间和登录IP
        $user['login_time'] = time();
        $user['login_ip']   = $this->request->ip();
        event('UserLogin', $user); // 登录成功后记录登录时间和IP
        $user['nonce'] = get_random_str();
        // 生成登录 token
        $token = $this->dataEncrypt($user);
        return ajax(200, '登录成功', ['token' => $token, 'type' => $type]);
    }

    /**
     * 用户注册
     * @author 贺强
     * @time   2021-10-21 10:39:10
     * @param  UserModel $u UserModel 实例
     */
    public function register(UserModel $u)
    {
        $param = $this->request->post();
        if (empty($param['mobile']) || empty($param['code'])) {
            return ajax(301, '参数缺失');
        }
        $mobile = $param['mobile'];
        $sc     = new SmsCodeModel();
        $time   = $sc->getFieldValue('ctime', ['mobile' => $mobile, 'code' => $param['code']]);
        if (empty($time)) {
            return ajax(302, '无效验证码');
        }
        $time += config('app.CODE_VALIDITY');
        if ($time < time()) {
            return ajax(303, '验证码过期');
        }
        $id   = $u->add(['mobile' => $mobile, 'username' => $mobile]);
        $data = ['id' => $id, 'mobile' => $mobile, 'username' => $mobile, 'login_time' => time(), 'login_ip' => $this->request->ip()];
        event('UserLogin', $data);
        $data['nonce'] = get_random_str();
        $sc->delByWhere(['mobile' => $mobile]);
        $token = $this->dataEncrypt($data); // 生成 token
        return ajax(200, '注册成功', ['token' => $token]);
    }
}
